<?php
/**
 *
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
 * @encoding UTF-8
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class Profil implements Page{
    public function main($args=NULL) {
        if (Session::getInstance()->sessionCheck()) {
            if (isset($args[0]) && $args[0]=='Edit') $this->delete ();
            else {
                $S = Template::getSmarty();
                $d = Database::select('Compte', '*', "id=".$_SESSION['id']);
                $S->assign("datas", $d);
                $S->display(Template::getPath("Profil"));
            }
        } else {
            header("Location: /index.php/");
        }
    }

    private function delete() {
            if (POST('suppr')) {
                $e1 = Storage::getInstance()->fetch()->exec("DELETE  FROM  Messages WHERE Sender=".$_SESSION["id"]." OR Receiver=".$_SESSION["id"]);
                $e2 = Storage::getInstance()->fetch()->exec("DELETE FROM  Compte WHERE id=".$_SESSION["id"]);
                $e3 = Storage::getInstance()->fetch()->exec("DELETE FROM  Tweet WHERE Profil=".$_SESSION["id"]);

                if ($e1 && $e2 && $e3) {
                    Session::getInstance()->sessionDestroy();
                    die ('Suppression reussie !  |  <a href="/index.php"> Home </a>');
                } else {
                    $this->Error("Impossible de supprimer le compte !");
                }
            }
            if (POST("pseudo")) {
                if (strlen($_POST['pseudo']) > 12) $this->Error("Pseudo Trop long !");
                if (Storage::getInstance()->fetch()->exec("UPDATE Compte SET pseudo=".Database::q($_POST['pseudo']). " WHERE id=".$_SESSION['id'])) {
                    die ('Mis à jour !');
                } else {
                    $this->Error("Pseudo non mis à jour !");
                }
            }
            if (POST("email")) {
                if (!preg_match("/^([a-zA-Z0-9.])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email'])) $this->Error("Email non conforme !");
                if (Storage::getInstance()->fetch()->exec("UPDATE Compte SET email=".Database::q($_POST['email']. " WHERE id=".$_SESSION['id']))) {
                    die ('Mis à jour !');
                } else {
                    $this->Error("Email non mis à jour !");
                }
            }
            

    }

    private function Error($msg) {
        $S = Template::getSmarty();
        $S->assign("Error", $msg);
        $S->display(Template::getPath("Profil"));
        die();
    }

}
?>
